package cn.inst.attend.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.inst.attend.dao.AttendMapper;
import cn.inst.attend.domain.Attend;
import cn.inst.attend.domain.AttendExample;
import cn.inst.attend.vo.PageCondition;
import cn.inst.common.page.PageQueryBean;
import cn.inst.common.untils.DateUntils;
@Service
public class AttendServiceImp implements AttendService {
	
	private static final int SEP_HOUR = 12;//上下午的分界时间(小时)
	private static final int SEP_MINUTE = 0;//上下午的分界时间(分钟)
	
	@Autowired
	private AttendMapper attendMapper;
//	private SimpleDateFormat format=new SimpleDateFormat("YY-MM-dd hh:mm");
	private Log log = LogFactory.getLog(AttendServiceImp.class);
	AttendExample attendExample = new AttendExample();
	/**
	 * 插入打卡信息
	 */
	@Override
	@Transactional
	public void signAttend(Attend attend) throws Exception{
		/**
		 * 判断今天打卡否
		 * 		true:有记录,判断上午or下午
		 * 				ture:上午,不作任何操作
		 * 				false:下午,更新时间
		 * 		fales:没记录,判断上午or下午
		 * 				ture:上午,插入上午时间
		 * 				false:下午,插入下午时间
		 */
		try {
			Date today = new Date();
			Date sepDate = DateUntils.getSepDate(SEP_HOUR, SEP_MINUTE);
			
			attend.setAttendData(today);//设置今天的时间
			Byte week = (byte) DateUntils.getWeek();
			attend.setAttendWeek(week);//设置今天是星期几
			
			//条件查询,查询今天是否已经打卡
			
			attendExample.clear();
			attendExample.createCriteria().andUserIdEqualTo(attend.getUserId()).andAttendDataEqualTo(attend.getAttendData());
			List<Attend> selectByExample = attendMapper.selectByExample(attendExample);
			
			if(!selectByExample.isEmpty()){
				//有记录,判断上午or下午
				if(today.compareTo(sepDate)<=0){
					//上午,不做任何操作
					return;
				}else {
					Attend updateAttend = selectByExample.get(0);
					updateAttend.setAttendEvening(today);
					attendMapper.updateByPrimaryKeySelective(updateAttend);
				}
				
			}else {
				//无记录,判断上午or下午
				if(today.compareTo(sepDate)<=0){
					//上午,插入上午时间
					attend.setAttendMoring(today);
				}else {
					//下午,插入下午时间
					attend.setAttendEvening(today);
				}
				//打卡记录插入
				attendMapper.insertSelective(attend);
			}
			
		} catch (Exception e) {
			log.error("打卡异常");
			throw e;
		}
		
	}
	
	
	/**
	 * 获取分页
	 * 	获得当前页i
	 * 	获取显示条数n
	 * 	(i-1)*n为开始,显示n条
	 * 获取分页
	 * private Integer currentPage;//当前页,前端传
	 * private Integer pageSize;//每页显示数据条数 ,前端传
	 * private int totalRows;//所有记录数,数据库查询
	 * private Integer startRow;//sql查询起始行,计算出
	 * private Integer totalPage;//总页数,计算出
	 * private List<?> items;//查询所得数据集,数据库查询
	 */
	@Override
	public PageQueryBean getPage(PageCondition pCondition) {
		Integer currentPage = pCondition.getCurrentPage();//当前页面
		Integer pageSize = pCondition.getPageSize();//每页显示数据条数
		
		Integer startRow=(currentPage-1)*pageSize;//开始的编号
		pCondition.setStartRow(startRow);
		//查询总记录数
		int totalRows=attendMapper.countPage(pCondition);	
		pCondition.setTotalPage(totalRows/pageSize);
		pCondition.setTotalRows(totalRows);
		//查询list-items
		List<Attend> list=attendMapper.selectByPageLimit(pCondition);
		pCondition.setItems(list);
		
		//查询的总记录数
		
		return pCondition;
	}

}
